package org.ourguy.mymoviesandshows;

import org.ourguy.mymoviesandshows.managers.DataAccessManager;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;

//TODO save state of DAM onStop
public class MyMoviesAndShowsActivity extends TabActivity {
	private DataAccessManager menager;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		menager = DataAccessManager.getInstance(this);

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost();  // The activity TabHost
		TabHost.TabSpec spec;  // Reusable TabSpec for each tab
		Intent intent;  // Reusable Intent for each tab

		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, TrackingListActivity.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("artists").setIndicator("Tracking List",
						  res.getDrawable(R.drawable.ic_heart_medium))
					  .setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		intent = new Intent().setClass(this, TrendingShowsActivity.class);
		spec = tabHost.newTabSpec("albums").setIndicator("Trends",
						  res.getDrawable(R.drawable.ic_chart_medium))
					  .setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, ScheduleActivity.class);
		spec = tabHost.newTabSpec("songs").setIndicator("Schedule",
						  res.getDrawable(R.drawable.ic_calendar_medium))
					  .setContent(intent);
		tabHost.addTab(spec);

		Intent i = getIntent();
		int a = i.getIntExtra("tab", 0);

		if(menager.getFavorites().size() == 0)
			tabHost.setCurrentTab(1);
		else
			tabHost.setCurrentTab(a);
		
		startService(new Intent(this, ReminderService.class));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
	
	//TODO CHECK IF TRACKING LIST IS EMPTY and select correct tab!!
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menuTrackingList:
	            getTabHost().setCurrentTab(0);
	            return true;
	        case R.id.menuSchedule:
	        	getTabHost().setCurrentTab(2);
	            return true;
	        case R.id.menuAbout:
	        	intent = new Intent()
	        		.setClass(getApplicationContext(), AboutActivity.class);
	        	this.startActivity(intent);
	        	return true;
	        case R.id.menuSearch:
	        	intent = new Intent()
	        		.setClass(getApplicationContext(), SearchActivity.class);
	        	this.startActivity(intent);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}